angular.module('myApp', ['infinite-scroll']);
//控制图片大小
var max = document.documentElement.clientWidth;

function initImg(obj) {
	obj.style.width = (max - 30) / 2.0 + "px";
	obj.style.height = (max - 30) / 2.0 + "px";
}

var val = document.getElementById("tex");
var texvalue = "";

function getProduct($scope, $http) {
	var uid = readData(localStorage.account);
	var gid = readData(localStorage.guideId);
	//搜索结果
	allgoodsUrl = getUrl("51getProduct");
	headerAll = getHeader();
	$scope.pages = 0;
	//默认加载第一页，初始值0
	$scope.goodsAll = [];
	$scope.totalpage = 1;
	$scope.search = function() {
			texvalue = val.value;
			$scope.pages = 0;
			$scope.goodsAll = [];
			plus.nativeUI.showWaiting('加载中...');
			//搜索加载前六条商品
			$http({
				method: 'POST',
				url: allgoodsUrl,
				headers: headerAll,
				timeout: 10000,
				//cache: true,
				data: 'indexpage=0&pageSize=6' + '&id=' + gid + '&name=' + texvalue
			}).success(function(data, status, headers, config) {
				if (data.obj) {
					//取的商品对象						
					var goodsList = data.obj.list;
					for (var i = 0; i < goodsList.length; i++) {
						if (typeof(goodsList[i]["picture"]) == "undefined") {
							goodsList[i]["picture"] = "../../image/shopping/middle.png";
						}
						if (goodsList[i]["pricename"]) {
							goodsList[i]["hide"] = true;
						} else {
							goodsList[i]["hide"] = false;
						}
						if (goodsList[i]["ruleflag"] == 0) {
							goodsList[i]["cu"] = false;
						} else {
							goodsList[i]["cu"] = true;
						}
						$scope.goodsAll.push(goodsList[i]);
					}
					//reloadHtml("dianpu.html",'');
					//plus.webview.getWebviewById("dianpu.html").reload(false);
					plus.nativeUI.closeWaiting();
					$scope.totalpage = data.obj.totalpage;
					//下拉页面+1
					$scope.pages = $scope.pages + 1;
				} else {
					$scope.goodsAll = [];
					mui.toast( '无此商品');
					plus.nativeUI.closeWaiting();
				};
			}).error(function() {
				plus.nativeUI.closeWaiting();
				console.log("网络连接异常，请稍后重试(e)");
			});
		}
		//总页数初始默认1，读取数据后再更新
	$scope.loadMore = function() {
		if (texvalue == "")
			return;
		if ($scope.pages < $scope.totalpage) {
			paramsAll = 'indexpage=' + $scope.pages + '&pageSize=6&id=' + gid + '&name=' + texvalue;
			$scope.pages = $scope.pages + 1;
			$http({
				method: 'POST',
				url: allgoodsUrl,
				headers: headerAll,
				timeout: 10000,
				//cache: true,
				data: paramsAll
			}).success(function(data, status, headers, config) {
				if (data.obj) {
					var goodsList = data.obj.list;
					for (var i = 0; i < goodsList.length; i++) {
						if (typeof(goodsList[i]["picture"]) == "undefined") {
							goodsList[i]["picture"] = "../../image/middle.png";
						}
						if (goodsList[i]["priceName"]) {
							goodsList[i]["hide"] = true;
						} else {
							goodsList[i]["hide"] = false;
						}
						if (goodsList[i]["ruleflag"] == 0) {
							goodsList[i]["cu"] = false;
						} else {
							goodsList[i]["cu"] = true;
						}
						$scope.goodsAll.push(goodsList[i]);
					}
					$scope.totalpage = data.obj.totalpage;
					//下拉页面+1
				} else {
					$scope.goodsAll = [];
					$scope.pages = $scope.pages - 1;
				};
			}).error(function() {
				console.log("网络连接异常，请稍后重试(e)");
				$scope.pages = $scope.pages - 1;
			});
		}
	}
	$scope.detail = function(id) {
		//获取当前商品ID 打开商品详情页面
		localStorage.goodsId = writeData(id);
		mui.openWindow({
			url: 'productDetail.html',
			id: 'productDetail.html'
		});
		setTimeout(function() {
			plus.webview.currentWebview().opener().close();
		}, 1000)
	}
}